package com.servlet.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.servlet.service.impl.AccountServiceImpl;
import com.servlet.service.impl.CarrierServiceImpl;

public class CarrierAction extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter pw = response.getWriter();
		boolean flag = false;
		String op = request.getParameter("op");
		try {
			if(null != op){
				if("AD".equals(op)){// 添加
					
					String acc = request.getParameter("acc");
					if(null != acc && !"".equals(acc)){
						request.setAttribute("acc", new AccountServiceImpl().getObjById(Long.parseLong(acc)));
						request.getRequestDispatcher("/WEB-INF/templates/frontpage/addCashCard.jsp").forward(request, response);
					}else{
						// 参数异常
						pw.write("参数异常！");
						pw.write("<input type=\"button\" value=\"＜＜返回\" style=\"cursor: pointer;\" onclick=\"window.history.back(-1);\">");
					}
					
				}else if("SA".equals(op)){// 保存
					
					flag = new CarrierServiceImpl().save(request);
					if(flag){//  保存成功跳转到 账户列表页面
						String acc = request.getParameter("acc");
						response.sendRedirect("AccountAction?op=SH&id="+acc);
						System.out.println("新增载体记录成功");
					}else{
						
						System.out.println("记账出错");
					}
					
				}else if("SH".equals(op)){// 展示页面
					
					
				}else if("LI".equals(op)){// 获取列表
					
				}else if("OBJS".equals(op)){// 修改
					
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(pw != null){
				pw.close();
			}
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
